﻿using System.Text;
using WFC.BL.Interfaces.DomainModels;

namespace WFC.DAL.StatementSources
{
    internal class InsertPostStatementSource : StatementSourceBase
    {
        internal InsertPostStatementSource(string title, string body, string shortDescription, string tags, int createdBy)
        {
            var stringBuilder = new StringBuilder();
            stringBuilder.Append("INSERT `wfc`.`Posts` (`Title`,`HtmlContent`,`ShortDescription`,`Tags`,`CreatedBy`,`CreateDttm`) ");
            stringBuilder.Append("VALUES (@Title,@HtmlContent,@ShortDescription,@Tags,@CreatedBy,NOW()); ");
            stringBuilder.Append("SELECT LAST_INSERT_ID();");

            Sql = stringBuilder.ToString();

            Parameters.AddWithValue("@Title", title);
            Parameters.AddWithValue("@HtmlContent", body);
            Parameters.AddWithValue("@ShortDescription", shortDescription);
            Parameters.AddWithValue("@Tags", tags);
            Parameters.AddWithValue("@CreatedBy", createdBy);
        }
    }

    internal class ReadByIdPostsStatementSource : StatementSourceBase
    {
        internal ReadByIdPostsStatementSource(int postId)
        {
            var stringBuilder = new StringBuilder();
            stringBuilder.Append("SELECT `Title`,`HtmlContent`,`ShortDescription`,`Tags`,`CreatedBy`,`CreateDttm`,`UpdatedBy`,`UpdateDttm` ");
            stringBuilder.Append("FROM `wfc`.`Posts` ");
            stringBuilder.Append("WHERE `Id`=@Id");

            Sql = stringBuilder.ToString();

            Parameters.AddWithValue("@Id", postId);
        }
    }

    internal class UpdatePostsStatementSource : StatementSourceBase
    {
        internal UpdatePostsStatementSource(IPostable post)
        {
            var stringBuilder = new StringBuilder();
            stringBuilder.Append("UPDATE `wfc`.`Posts` SET `Title`=@Title,`HtmlContent`=@HtmlContent,`ShortDescription`=@ShortDescription,`Tags`=@Tags,`UpdatedBy`=@UpdatedBy,`UpdateDttm`=NOW() ");
            stringBuilder.Append("WHERE `Id`=@Id");

            Sql = stringBuilder.ToString();

            Parameters.AddWithValue("@Id", post.Id);
            Parameters.AddWithValue("@Title", post.Title);
            Parameters.AddWithValue("@HtmlContent", post.HtmlContent);
            Parameters.AddWithValue("@ShortDescription", post.ShortDescription);
            Parameters.AddWithValue("@Tags", post.Tags);
            Parameters.AddWithValue("@UpdatedBy", post.UpdatedBy);
        }
    }

    internal class DeletePostsStatementSource : StatementSourceBase
    {
        internal DeletePostsStatementSource(int postId)
        {
            Sql = "DELETE FROM `wfc`.`Posts` WHERE `Id`=@Id";

            Parameters.AddWithValue("@Id", postId);
        }
    }
}
